package com.marktotop.controller;


import com.marktotop.entity.Admin;
import com.marktotop.entity.Role;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IAdminService;
import com.marktotop.service.IAdminVOService;
import com.marktotop.service.IRoleService;
import com.marktotop.vo.AdminPassVO;
import com.marktotop.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marktotop
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/system/admin")
@Api(tags = "操作员控制接口")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IAdminVOService adminVOService;

    @Autowired
    private IRoleService roleService;

    /**
     * 获取所有操作员
     *
     * @param keywords
     * @return
     */
    @ApiOperation(value = "获取所有操作员")
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ResultBean getAllAdmins(String keywords){
        List<Admin> allAdmins = adminService.getAllAdmins(keywords);
        if (null != allAdmins){
            return ResultBean.ok().message("信息查询成功！").data("allAdmins",allAdmins);
        }
        return ResultBean.error().message("查询所有操作员失败！");
    }

    /**
     * 更新操作员信息
     *
     * @param adminVO
     * @return
     */
    @ApiOperation(value = "更新操作员")
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    public ResultBean updateAdmin(@RequestBody AdminVO adminVO){
        if (adminVOService.updateById(adminVO)){
            return ResultBean.ok().message("管理员信息更新成功！");
        }
        return ResultBean.error().message("管理员信息更新失败！");
    }

    @ApiOperation(value = "删除操作员操作")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteAdmin(@PathVariable("id") Integer id){
        if (adminService.removeById(id)){
            return ResultBean.ok().message("成功删除操作员!");
        }
        return ResultBean.error().message("删除操作员操作失败！");
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @ApiOperation(value = "获取所有角色")
    @RequestMapping(value = "/roles",method = RequestMethod.GET)
    public List<Role> getAllRoles(){
        return roleService.list();
    }

    /**
     * 更新操作员角色
     *
     * @param adminId
     * @param rids
     * @return
     */
    @ApiOperation(value = "更新操作员角色")
    @RequestMapping(value = "/role",method = RequestMethod.PUT)
    public ResultBean updateAdminRole(Integer adminId, Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }


    @ApiOperation(value = "修改登录操作员密码")
    @RequestMapping(value = "/pass",method = RequestMethod.POST)
    public ResultBean updateAdminPassword(@RequestBody AdminPassVO adminPassVO){
        return adminService.updateAdminPass(adminPassVO.getAdminId(),adminPassVO.getOldPass(),adminPassVO.getNewPass());
    }

}
